package com.gdth.bank.model;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import com.gdth.bank.BankApplication;
import com.gdth.bank.bean.ContactBean;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts.Photo;
import rx.Observable;
import rx.Subscriber;

public class ContactModel { 
	/**
	 * 读取通讯录
	 * @return
	 */
	public Observable<List<ContactBean>> getContactList(){
		return Observable.create(new Observable.OnSubscribe<List<ContactBean>>() {

			@Override
			public void call(Subscriber<? super List<ContactBean>> arg0) {
				// TODO Auto-generated method stub
				List<ContactBean> mList = getPhoneContacts();
				
				if (mList == null){
					arg0.onError(null);
				} else {
					arg0.onNext(mList);
					arg0.onCompleted();
				}
			}
		});
	}
	
	/**
	 * 查询字段
	 */
	private static final String[] PHONES_PROJECTION = new String[] {  
			Phone.DISPLAY_NAME, Phone.NUMBER, Photo.PHOTO_ID,Phone.CONTACT_ID, Phone.SORT_KEY_PRIMARY, Phone.LOOKUP_KEY, Phone.LAST_TIME_CONTACTED }; 
	
	private List<ContactBean> getPhoneContacts() {  
		List<ContactBean> list = null;
		ContentResolver resolver = BankApplication.mInstance.getBaseContext().getContentResolver();  

		// 获取手机联系人  
		Cursor cursor = resolver.query(Phone.CONTENT_URI, PHONES_PROJECTION, null, null, Phone.SORT_KEY_PRIMARY);  
		
		if (cursor != null) {
			list = new ArrayList<ContactBean>();
			int display_name = cursor.getColumnIndex("display_name");
			int data1 = cursor.getColumnIndex("data1");
			int photo_id = cursor.getColumnIndex("photo_id");
			int contact_id = cursor.getColumnIndex("contact_id");
//			int last_time_contacted = cursor.getColumnIndex("last_time_contacted");
			int sort_key = cursor.getColumnIndex("sort_key");
			int lookup = cursor.getColumnIndex("lookup");
			while (cursor.moveToNext()) {
				int contactId = cursor.getInt(contact_id);
				String name = cursor.getString(display_name);
				String number = cursor.getString(data1);
				Long photoId = cursor.getLong(photo_id);
				String lookUpKey = cursor.getString(lookup);
				String sortKey = cursor.getString(sort_key);
				
				// 创建联系人对象
				ContactBean contact = new ContactBean();
				contact.setDesplayName(name);
				contact.setPhoneNum(number);
				contact.setSortKey(sortKey);
				contact.setPhotoId(photoId);
				contact.setLookUpKey(lookUpKey);
				contact.setContactId(contactId);
				list.add(contact);

				//得到联系人头像Bitamp  
				Bitmap contactPhoto = null;  
	
				//photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的  
				if(photoId > 0 ) {  
					Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, photoId);  
					InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(resolver, uri);  
					contactPhoto = BitmapFactory.decodeStream(input);  
				}else {  
//					contactPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.contact_photo);  
				}
				
				contact.setContactPhoto(contactPhoto);
			}
			cursor.close();  
		}
		
		return list;
	}
}
